import { Component, OnInit } from '@angular/core';
import { ModalService } from './modal.service';
import { MessageBoxResult } from './messageBoxResult';

declare const $: any;

@Component({
    selector: 'modal',
    templateUrl: 'app/component/modal/modal.component.html'
})
export class ModalComponent implements OnInit {
    constructor(private modalSvr: ModalService) { }

    private messageResult: MessageBoxResult;
    private modalElement: any;

    title: string ;
    content: string;
    ngOnInit() {
        this.modalElement = $('#baseModal');
        this.modalSvr.OnShow = (title, content) => {
            this.title = title;
            this.content = content;
            this.modalElement.modal('show');
            return this.GetResult();
        }
    }

    private GetResult() {
        return new Promise((resolve, reject) => {
            this.modalElement.on('hidden.bs.modal', (e: any) => {
                resolve(this.messageResult);
            });
        });
    }

    ok_click() {
        this.messageResult = MessageBoxResult.Ok;
        this.modalElement.modal('hide');
    }

    cancel_click() {
        this.messageResult = MessageBoxResult.Cancel;
        this.modalElement.modal('hide');
    }
}